package org.web2024.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CaculateUtil {
    private static final String SEMESTER_PATTERN = "^\\d+年[春夏秋]$";

    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";

    public static boolean isValidDateTime(String dateTime) {
        try {
            // 尝试解析日期时间
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
            LocalDateTime.parse(dateTime, formatter);
            return true;
        } catch (DateTimeParseException e) {
            // 如果日期时间格式不匹配或无效，抛出异常
            return false;
        }
    }

    public static boolean isValidSemester(String semester) {
        // 创建 Pattern 对象
        Pattern pattern = Pattern.compile(SEMESTER_PATTERN);
        // 创建 Matcher 对象
        Matcher matcher = pattern.matcher(semester);
        // 返回是否匹配
        return matcher.matches();
    }

    public static int getTermScore(String term) {
        String[] parts = term.split("年");
        int year = Integer.parseInt(parts[0]);  // 例如：22
        String season = parts[1].trim();  // 例如：春
        int seasonValue = 0;
        switch (season) {
            case "春":
                seasonValue = 1;
                break;
            case "秋":
                seasonValue = 2;
                break;
            case "夏":
                seasonValue = 3;
                break;
            default:
                throw new IllegalArgumentException("未知季节：" + season);
        }
        return year * 10 + seasonValue;
    }


}
